//
//  FQOtherQuestionController.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/9/5.
//

import UIKit

class FQOtherQuestionController: BaseListSectionController, ListSupplementaryViewSource {
    var model : FQOtherQuestionModel!
    
    override init() {
        super.init()
        supplementaryViewSource = self
        inset = UIEdgeInsets(top: 0, left: 16, bottom: 16, right: 16)
    }
    
    override func numberOfItems() -> Int {
        model.list.count
    }
    
    func supportedElementKinds() -> [String] {
        [UICollectionView.elementKindSectionHeader]
    }
    
    func viewForSupplementaryElement(ofKind elementKind: String, at index: Int) -> UICollectionReusableView {
        switch elementKind {
        case UICollectionView.elementKindSectionHeader:
            return collectionContext!.dequeueReusableSupplementaryView(ofKind: elementKind, for: self, class: FQOtherQuestionCollectionReusableView.self, at: index)
        default:
            fatalError()
        }
    }
    
    func sizeForSupplementaryView(ofKind elementKind: String, at index: Int) -> CGSize {
        CGSize(width: SCREEN_WIDTH, height: 50)
    }
    
    override func sizeForItem(at index: Int) -> CGSize {
        CGSize(width: SCREEN_WIDTH - 32, height: 50)
    }
    
    override func cellForItem(at index: Int) -> UICollectionViewCell {
        let cell = dequeueReusableCell(cellType: FQOtherQuestionCollectionViewCell.self, index: index)
        cell.model = model.list[index]
        cell.islast = model.list.count - 1 == index
        return cell
    }
    
    override func didSelectItem(at index: Int) {
        let cell = collectionContext!.cellForItem(at: index, sectionController: self) as! FQOtherQuestionCollectionViewCell
        (viewController as! FQHelpCenterDetailViewController).reload(question: cell.model.question)
    }
    
    override func didUpdate(to object: Any) {
        model = object as? FQOtherQuestionModel
    }
}
